<template>
  <div class="GHighriskMobileList">
    <filterPanel filterName="GHighriskMobileListFilter" filterTitle="高危电话查询" :filterList="filterData"
                 @filterChange="filterChange"></filterPanel>
    <tablePanel tableName="GHighriskMobileListTable" :tableLoading="loading" tableTitle="高危电话列表" :tableList="tableList"
                :tableData="tableData" :total="total" :currentPage="form.page_start" :pageSize="form.page_size"
                @sizeChange="sizeChange" @pageChange="pageChange" @tableClick="tableClick">
      <zy-button @click="add" type="primary">新增</zy-button>
      <zy-button @click="batchImport">批量导入</zy-button>
      <zy-button @click="exportExcel">导出列表</zy-button>
    </tablePanel>
    <zyModel :show="showModel" :title="modelTitle" @submit="submit" @close="closeModel">
      <div class="personalChange">
        <zy-modal-info :model-info="modelInfo" cell-num="2"></zy-modal-info>
      </div>
    </zyModel>
    <commonModel :show="show" type="mobile" @close="close"></commonModel>
  </div>
</template>

<script>
	import {mapGetters} from "vuex";
	import commonModel from '../common/commonModel'

	export default {
		name: "GHighriskMobileList",
		props: {},
		data() {
			return {
				showModel: false,
				modelTitle: '',
				modelInfo: [
					{
						title: '电话号码',
						name: 'judge_phone',
						type: 'input',
						value: '',
						maxlength: '20',
						readonly: false,
						titleClass: 'required'
					},
					{
						title: '关注等级',
						name: 'judge_level',
						type: 'select',
						value: 1,
						optionList: [
							{label: '1', value: '1'},
							{label: '2', value: '2'},
							{label: '3', value: '3'},
							{label: '4', value: '4'},
							{label: '5', value: '5'},
						],
						readonly: false,
						titleClass: 'required'
					},
					{
						title: '是否标记可疑件',
						name: 'is_dubious',
						type: 'radio',
						value: '',
						optionList: [
							{label: '否', value: '0'},
							{label: '是', value: '1'},
						],
						readonly: false,
						titleClass: 'required'
					},
					{
						title: '预警方式',
						name: 'early_type',
						type: 'checkbox',
						value: [],
						optionList: [
							{label: '短信', value: '1'},
							{label: '站内信', value: '2'},
						],
						readonly: false,
						titleClass: ''
					},
					{
						title: '预警人员',
						name: 'police_ids',
						type: 'selectMultiple',
						value: '',
						showWord: true,
						optionList: [],
						readonly: false,
						titleClass: '',
					},
				],
				currentPath: '/judge_mobile_list',
				filterData: [
					{
						title: '电话号码',
						name: 'judge_phone',
						type: 'input',
						value: '',
						maxlength: '20',
						isShow: true,
						isSelected: true
					},
					{
						title: '号码归属地',
						name: 'number_attr',
						type: 'input',
						value: '',
						isShow: true,
						isSelected: true
					},
					{
						title: '关注等级',
						name: 'judge_level',
						type: 'select',
						optionList: [
							{label: '全部', value: ''},
							{label: '1', value: '1'},
							{label: '2', value: '2'},
							{label: '3', value: '3'},
							{label: '4', value: '4'},
							{label: '5', value: '5'},
						],
						value: '',
						isShow: true,
						isSelected: true
					},
					{
						title: '是否标记可疑件',
						name: 'is_dubious',
						type: 'select',
						optionList: [
							{label: '全部', value: ''},
							{label: '是', value: '1'},
							{label: '否', value: '0'},
						],
						value: '',
						isShow: true,
						isSelected: true
					},
					{
						title: '预警方式',
						name: 'early_type',
						type: 'select',
						optionList: [
							{label: '全部', value: ''},
							{label: '短信', value: '1'},
							{label: '站内信', value: '2'},
						],
						value: '',
						isShow: true,
						isSelected: true
					},
					{
						title: '预警人员',
						name: 'plc_name',
						type: 'select',
						optionList: [],
						value: '',
						isShow: true,
						isSelected: true
					},
					{
						title: '创建时间',
						name: 'time',
						type: 'date',
						value: [],
						isShow: true,
						isSelected: true
					},
				],
				form: {
					judge_phone: '',
					number_attr: '',
					judge_level: '',
					is_dubious: '',
					early_type: '',
					plc_name: '',
					time: [],
					page_start: 1,
					page_size: 10
				},
				loading: false,
				tableList: [
					{
						title: '电话号码',
						width: 120,
						name: 'judge_phone',
						isShow: true,
						isSelected: true
					},
					{
						title: '号码归属地',
						width: 120,
						name: 'number_attr',
						isShow: true,
						isSelected: true
					},
					{
						title: '关注等级',
						width: 200,
						name: 'judge_level',
						isShow: true,
						isSelected: true
					},
					{
						title: '是否标记可疑件',
						width: 150,
						name: 'is_dubious_desc',
						isShow: true,
						isSelected: true
					},
					{
						title: '预警方式',
						width: 120,
						name: 'early_type_str',
						isShow: true,
						isSelected: true
					},
					{
						title: '预警人员',
						width: 120,
						name: 'police_names',
						isShow: true,
						isSelected: true
					},
					{
						title: '创建时间',
						width: 200,
						name: 'create_time_cn',
						isShow: true,
						isSelected: true
					},
					{
						title: '操作',
						width: 200,
						name: 'handler',
						fixed: 'right',
						btns: [
							{
								type: 'edit',
								name: '编辑'
							},
							{
								type: 'delete',
								name: '删除',
								btnType: 'danger'
							},
						],
						isSelected: true
					}
				],
				tableData: [],
				total: 0,
				download_url: '',
				judge_phone_id: '',
				show: false
			};
		},
		computed: {
			postData() {
				let data = {
					judge_phone: this.form.judge_phone,
					number_attr: this.form.number_attr,
					judge_level: this.form.judge_level,
					is_dubious: this.form.is_dubious,
					early_type: this.form.early_type,
					plc_name: this.form.plc_name,
					pagination: {
						page_start: this.form.page_start,
						page_size: this.form.page_size
					}
				};
				if (this.form.time && this.form.time.length > 0) {
					data.start_created_time = this.form.time[0] / 1000;
					data.end_created_time = this.form.time[1] / 1000;
				}
				return data;
			},
			...mapGetters(['authority']),
			isAdd() {
				return this.authority[this.currentPath] && this.authority[this.currentPath].i;
			},
			isEdit() {
				return this.authority[this.currentPath] && this.authority[this.currentPath].m;
			},
			isDel() {
				return this.authority[this.currentPath] && this.authority[this.currentPath].d;
			},
		},
		watch: {},
		components: {commonModel},
		methods: {
			close() {
				this.show = false
			},
			getPoliceData() {
				this.$axios.post(this.$api.g_listContactPolice, {
					pagination: {
						page_start: 1,
						page_size: 9999
					}
				}).then(res => {
					let {code, msg, data} = res;
					if (code == 200) {
						let list = JSON.parse(JSON.stringify(data.list));
						this.setModelInfo({
							police_ids: list.filter(item => {
								item.value = String(item.id)
								item.label = item.name
								return item;
							}),
						}, ['police_ids']);
						this.setFilterData({
							plc_name: data.list.filter(item => {
								item.value = item.name;
								item.label = item.name
								return item;
							}),
						}, ['plc_name']);
					} else {
						this.$message.error(msg);
					}
				}).catch(ret => {
					this.$message.error(ret.msg || '服务繁忙，请稍后再试');
				});
			},
			setFilterData(val, nameList) {
				for (let item of this.filterData) {
					if (nameList.indexOf(item.name) !== -1) {
						item.optionList = val[item.name]
					}
				}
			},
			setModelInfo(val, nameList) {
				for (let item of this.modelInfo) {
					if (nameList.indexOf(item.name) !== -1) {
						item.optionList = val[item.name]
					}
				}
			},
			add() {
				this.showModel = true;
				this.modelTitle = '新增高危电话';
			},
			batchImport() {
				this.show = true;
			},
			//导出
			exportExcel() {
				this.$import_excal(this.download_url || "");
			},
			sizeChange(val) {
				this.form.page_size = val;
				this.getData()
			},
			pageChange(val) {
				this.form.page_start = val;
				this.getData()
			},
			filterChange(data) {
				this.form = {...this.form, ...data};
				this.getData();
			},
			getData() {
				this.loading = true;
				this.tableData = [];
				this.$axios.post(this.$api.g_listPhone, {...this.postData}).then(res => {
					let {code, msg, data} = res;
					this.loading = false;
					if (code == 200) {
						let arr1 = ['否', '是'];
						this.tableData = data.list.filter(item => {
							item.is_dubious_desc = arr1[item.is_dubious];
							item.edit = this.isEdit || true;
							item.delete = this.isDel || true;
							return item;
						});
						this.download_url = data.download_url || ''
						this.total = data.pagination.total_count || 0;
					} else {
						this.total = 0
						this.tableData = [];
					}
				}).catch(ret => {
					this.$message.error(ret.msg || '服务繁忙，请稍后再试');
				});
			},
			tableClick(data, type) {
				let {judge_phone_id} = data;
				switch (type) {
					case 'edit':
						this.toEdit(data);
						break;
					case 'delete':
						this.deleted(judge_phone_id)
						break;
				}
			},
			toEdit(data) {
				this.showModel = true;
				this.modelTitle = '编辑高危电话';
				this.judge_phone_id = data.judge_phone_id;
				this.modelInfo = this.modelInfo.filter(item => {
					for (var key in data) {
						if (item.name == 'is_dubious' || item.name == 'judge_level') {
							item.value = String(data[item.name]) || '';
						} else if (item.name == 'early_type') {
							item.value = data[item.name] ? [...data[item.name]] : []
						} else if (item.name == 'police_ids') {
							item.value = data[item.name] && data[item.name].length ? data[item.name].split(',') : []
						} else {
							item.value = data[item.name];
						}
					}
					return item
				});
			},
			submit() {
				let postData = {
					judge_phone_id: this.judge_phone_id
				};
				this.modelInfo.forEach(item => {
					if (!item.readonly) {
						if (item.name == 'early_type' || item.name == 'police_ids') {
							postData[item.name] = item.value.join();
						} else {
							postData[item.name] = item.value;
						}
					}
				});
				this.$axios.post(this.$api.g_entryPhone, {
					...postData
				}).then(res => {
					let {code, msg, data} = res;
					this.loading = false;
					if (code == 200) {
						this.closeModel();
						this.getData();
					} else {
						this.$message.error(msg);
					}
				}).catch(ret => {
					this.$message.error(ret.msg || '服务繁忙，请稍后再试');
				});
			},
			closeModel() {
				this.showModel = false;
				this.judge_phone_id = '';
				this.modelInfo.filter(item => {
					if (item.name == 'early_type' || item.name == 'police_ids') {
						item.value = []
					} else {
						item.value = ''
					}
				})
			},
			// 删除
			deleted(judge_phone_id) {
				this.$confirm("此操作将删除该高危电话, 是否继续?", "提示", {
					confirmButtonText: "确定",
					cancelButtonText: "取消",
					type: "warning",
					customClass: 'message',
				}).then(() => {
					this.$axios.post(this.$api.g_delPhone, {judge_phone_id}).then(res => {
						let {code, msg} = res;
						if (code === 200) {
							this.$message.success("删除成功！");
							this.getData();
						} else {
							this.$message.error(msg);
						}
					})
				}).catch(() => {
					this.$message.info("已取消删除");
				});
			},
		},
		mounted() {
			this.getData();
			this.getPoliceData();
		},
		created() {
		}
	}
</script>

<style scoped lang="less" src="@/style/government/judgeManage/highriskMobile/index.less">

</style>
